<template>
    <div class="chart-wrap">
       <div class="title">
          <span v-text="name"></span>
          <div class="tools">
            <el-select v-model="query.dateType" placeholder="请选择">
              <el-option
                v-for="item in dateTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <el-date-picker
              v-model="query.startDate"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
            </el-date-picker>
            <el-date-picker
              v-model="query.endDate"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </div>
       </div>
       <div class="chart">
           <div ref="chart" class="content" ></div>
       </div>
    </div>
</template>

<script>
    import mixin from '../mixs/mixin.js';
    import echarts from "echarts";
    export default {
        name: "chart_1" ,
        mixins: [mixin],
        props: {
          name: {
            type: String,
            default: '表名称'
          },
          category:{
            type: String,
            default: 'News' //分类 News,Paper,Patent
          }
        },
        data() {
          let startDate = new Date(`${new Date().getFullYear()}/1/1`).Format('yyyy-MM-dd');
          let endDate = new Date().Format('yyyy-MM-dd');
          return {
            apiUrl:'organalyse/allOrgCooperateNet',
            dateTypeList:[
              {label:'月',value:'Month'},
              {label:'天',value:'Day'}
            ],
            query:{
              startDate:startDate, //开始时间
              endDate:endDate, //结束时间
              dateType:'Month', //Month,Day 查询时间类型
              category:'News', //分类 News,Paper,Patent
            },
            data:[],
            links:[]
          }
        },
        mounted(){
          this._initChart();
        },
        methods: {
          _initChart() {
             this.$nextTick(()=>{
                 if(!this._vmEchart){
                   this._vmEchart = echarts.init(this.$refs.chart);
                   this._vmEchart.showLoading();
                 }
             })
          },
          asyncDate(){
            //  异步加载数据  通用方法
            if(!this._vmEchart) return;
            this._vmEchart.clear();
            this._vmEchart.showLoading();
            this.api({
              url: this.apiUrl,
              method: "post",
              data:this.$qs.stringify({...this.query,topicId:this.navCurrentId})
            }).then(data => {
               this._formatDate(data)
            });
          },
          getDataURL(){
            // 获取图片base64
            return new Promise((resolve,reject)=>{
              if(!this._vmEchart){reject(); return; }
              resolve(this._vmEchart.getDataURL({
                backgroundColor: this.exportEchartBg
              }))
            });
          },
          _formatDate(data){
            // 根据不同的数据 配合不同的方法
            let nodes = [];
            data.nodes.map(function(node) {
              let datanode = {
                category:0,
                id: node.id + '',
                name: node.label,
                value: node.weight,
                symbolSize: 25,
                itemStyle: {
                  normal: {
                  }
                }
              };
              nodes.push(datanode);
            });
            let links = [];
            data.edges.map(function(edge) {
              let dataedge = {
                source: edge.sourceID + '',
                target: edge.targetID + '',
                lineStyle:{
                  width: 1,
                  color: '#000'
                },
                label: {
                  normal: {
                    show: true,
                    formatter: '合作'
                  }
                },
              };
              links.push(dataedge);
            });

            this.data = nodes;
            this.links = links;
            this._setOption();
          },
          _setOption(){
             let option = {
               title: {
                 text: ''
               },
               tooltip: {},
               animationDurationUpdate: 1500,
               animationEasingUpdate: 'quinticInOut',
               label: {
                 normal: {
                   show: true,
                   textStyle: {
                     fontSize: 12
                   },
                 }
               },
               legend: {
                 x: "center",
                 show: false,
                 data: ["科研机构"]
               },
               series: [

                 {
                   type: 'graph',
                   layout: 'force',
                   symbolSize: 45,
                   focusNodeAdjacency: true,
                   roam: true,
                   categories: [
                     {
                     name: '科研机构',
                     itemStyle: {
                       normal: {
                         color: "#3592FF",
                       }
                     }
                   }],
                   label: {
                     normal: {
                       show: true,
                       textStyle: {
                         fontSize: 12
                       },
                     }
                   },
                   force: {
                     repulsion: 3400
                   },
                   edgeSymbolSize: [4, 50],
                   edgeLabel: {
                     normal: {
                       show: true,
                       textStyle: {
                         fontSize: 10
                       },
                       formatter: "{c}"
                     }
                   },
                   data: this.data,
                   links: this.links,
                   lineStyle: {
                     normal: {
                       opacity: 0.9,
                       color:'#000',
                       width: 1,
                       curveness: 0
                     }
                   }
                 }
               ]
             };

              this._vmEchart.hideLoading();
              this._vmEchart.setOption(option)
          }
        },


    }
</script>

<style lang="scss" scoped>
    .chart{
      position: relative;
      .content{
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
      }
    }

</style>
